<?php

require_once("../pdo/exams.php");

class ExamsTest
{
	public $Exams;
	
	public function test_get_by_instance($instance)
	{
		return $this->Exams->get_by_instance($instance);
	}

	public function test_insert($instance,$date,$start_time,$end_time,$location)
	{
		return $this->Exams->insert($instance,$date,$start_time,$end_time,$location);
	}
	
	public function test_update($exam,$date,$start_time,$end_time,$location)
	{
		return $this->Exams->update($exam,$date,$start_time,$end_time,$location);
	}
	
	public function test_delete($exam)
	{
		return $this->Exams->delete($exam);
	}
	
	public function test($pdo,$instance,$date1,$start_time1,$end_time1,$location1,$date2,$start_time2,$end_time2,$location2)
	{
		$this->Exams = new Exams(); $this->Exams->pdo = $pdo;
	
		echo "Kokeillaan muokata kokeita arvoilla<ul>";
		echo "<li>instance = " . $instance . "</li>";
		echo "<li>date = " . $date1 . " ja " . $date2 . "</li>";
		echo "<li>start_time = " . $start_time1 . " ja " . $start_time2 . "</li>";
		echo "<li>end_time = " . $end_time1 . " ja " . $end_time2 . "</li>";
		echo "<li>location = " . $location1 . " ja " . $location2 . "</li>";
		echo "</ul><ol>";
		
		if($this->test_insert($instance,$date1,$start_time1,$end_time1,$location1))
			echo "<li>Lisäys onnistui!</li>";
		
		$exams = $this->test_get_by_instance($instance);		
		$id = null;	
		foreach($exams as &$exam)
			if($exam->date       == strtotime($date1 . " " . $start_time1)
			&& $exam->start_time == strtotime($date1 . " " . $start_time1)
			&& $exam->end_time   == strtotime($date1 . " " . $end_time1)
			&& $exam->location   == $location1)
			{
				$id = $exam->id;
				break;
			}
		if($id != null)
			echo "<li>Lisätty koe löytyi!</li>";
		
		if($this->test_update($id,$date2,$start_time2,$end_time2,$location2))
			echo "<li>Muokkaus onnistui!</li>";
		
		$exams = $this->test_get_by_instance($instance);
		$id = null;	
		foreach($exams as &$exam)
			if($exam->date       == strtotime($date2 . " " . $start_time2)
			&& $exam->start_time == strtotime($date2 . " " . $start_time2)
			&& $exam->end_time   == strtotime($date2 . " " . $end_time2)
			&& $exam->location   == $location2)
			{
				$id = $exam->id;
				break;
			}
		if($id != null)
			echo "<li>Muokattu koe löytyi!</li>";
		
		if($this->test_delete($id))
			echo "<li>Poisto onnistui!</li>";
		
		$exams = $this->test_get_by_instance($instance);
		$id = null;
		foreach($exams as &$exam)
			if($exam->date       == strtotime($date2 . " " . $start_time2)
			&& $exam->start_time == strtotime($date2 . " " . $start_time2)
			&& $exam->end_time   == strtotime($date2 . " " . $end_time2)
			&& $exam->location   == $location2)
			{
				$id = $exam->id;
				break;
			}
		if($id == null)
			echo "<li>Poistettu koe hävisi taulukosta!</li>";		
		echo "</ol>";
	}
}

?>
